package com.wokeMy.view.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wokeMy.view.model.News;
import com.zhongcai.online.R;

import java.util.ArrayList;

/**
 * Created by zhengpeng on 2018/4/24.
 */

public class NewsAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private ArrayList<News> data;
    private Context context;

    public NewsAdapter(Context context,ArrayList<News> data) {
        this.inflater = LayoutInflater.from(context);
        this.data=data;
        this.context=context;
    }

    public void setData(ArrayList<News> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold=null;
        if (convertView==null){
            viewHold=new ViewHold();
            convertView = inflater.inflate(R.layout.adapter_news_viewpage, parent, false);
            viewHold.newTitle=(TextView) convertView.findViewById(R.id.title_news);
            viewHold.time=convertView.findViewById(R.id.time_news);
            viewHold.newdesc=convertView.findViewById(R.id.content_news);
            viewHold.newIv=convertView.findViewById(R.id.iv_news);
            convertView.setTag(viewHold);

        }else {
            viewHold=(ViewHold)convertView.getTag();
        }
        News news=(News)getItem(position);
        viewHold.newTitle.setText(news.getTitle());
        viewHold.newdesc.setText(news.getDesc());
        viewHold.time.setText(news.getCreate_time());
        Glide.with(convertView).asBitmap().load(news.getImgurl()).into(viewHold.newIv);
        return convertView;
    }
    class  ViewHold {
        TextView newTitle;
        TextView newdesc;
        ImageView newIv;
        TextView time;
    }
}
